package com.hl.qzzpbackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hl.qzzpbackend.exception.BusinessException;
import com.hl.qzzpbackend.exception.ErrorCode;
import com.hl.qzzpbackend.exception.ThrowUtils;
import com.hl.qzzpbackend.model.dto.application.ApplicationAddRequest;
import com.hl.qzzpbackend.model.dto.application.ApplicationQueryRequest;
import com.hl.qzzpbackend.model.dto.application.ApplicationUpdateStatusRequest;
import com.hl.qzzpbackend.model.entity.Application;
import com.hl.qzzpbackend.model.entity.Job;
import com.hl.qzzpbackend.model.entity.Resume;
import com.hl.qzzpbackend.model.entity.User;
import com.hl.qzzpbackend.model.enums.ApplicationStatusEnum;
import com.hl.qzzpbackend.model.vo.ApplicationVO;
import com.hl.qzzpbackend.service.ApplicationService;
import com.hl.qzzpbackend.mapper.ApplicationMapper;
import com.hl.qzzpbackend.service.JobService;
import com.hl.qzzpbackend.service.ResumeService;
import com.hl.qzzpbackend.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 求职申请业务层
 * @author helong
 */
@Service
public class ApplicationServiceImpl extends ServiceImpl<ApplicationMapper, Application>
    implements ApplicationService{

    @Resource
    private UserService userService;

    @Resource
    private JobService jobService;

    @Resource
    private ResumeService resumeService;

    @Resource
    private ApplicationMapper applicationMapper;

    /**
     * 新增职位申请
     * @param applicationAddRequest 新增职位申请请求类
     * @return 是否成功
     */
    @Override
    public boolean applicationAdd(ApplicationAddRequest applicationAddRequest) {
        Long jobId = applicationAddRequest.getJobId();
        Long userId = applicationAddRequest.getUserId();
        Long resumeId = applicationAddRequest.getResumeId();
        Integer status = applicationAddRequest.getStatus();
        ApplicationStatusEnum applicationStatusEnum = ApplicationStatusEnum.getEnumByValue(status);

        // 参数校验
        ThrowUtils.throwIf(applicationStatusEnum == null, ErrorCode.PARAMS_ERROR,"职位申请状态错误");
        long count1 = userService.lambdaQuery().eq(User::getId,userId).count();
        ThrowUtils.throwIf(count1 == 0, ErrorCode.PARAMS_ERROR,"用户不存在");
        long count2 = jobService.lambdaQuery().eq(Job::getId,jobId).count();
        ThrowUtils.throwIf(count2 == 0, ErrorCode.PARAMS_ERROR,"职位不存在");
        long count3 = resumeService.lambdaQuery().eq(Resume::getId,resumeId).count();
        ThrowUtils.throwIf(count3 == 0, ErrorCode.PARAMS_ERROR,"简历不存在");

        Application application = new Application();
        BeanUtil.copyProperties(applicationAddRequest,application);

        boolean result = this.save(application);
        if(!result){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"新增申请职位失败");
        }
        return true;
    }


    /**
     * 更新职位申请状态
     * @param applicationUpdateStatusRequest 更新职位申请状态请求类
     * @return 是否成功
     */
    @Override
    public boolean applicationUpdateStatus(ApplicationUpdateStatusRequest applicationUpdateStatusRequest) {
        Long id = applicationUpdateStatusRequest.getId();
        Integer status = applicationUpdateStatusRequest.getStatus();
        ApplicationStatusEnum applicationStatusEnum = ApplicationStatusEnum.getEnumByValue(status);

        // 参数校验
        ThrowUtils.throwIf(applicationStatusEnum == null, ErrorCode.PARAMS_ERROR,"职位申请状态错误");
        long count = this.lambdaQuery().eq(Application::getId,id).count();
        ThrowUtils.throwIf(count == 0, ErrorCode.PARAMS_ERROR,"职位申请不存在");

        Application application = new Application();
        BeanUtil.copyProperties(applicationUpdateStatusRequest,application);

        boolean result = this.updateById(application);
        if(!result){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"更新申请职位状态失败");
        }
        return true;
    }

    /**
     * 筛选简历
     * @param applicationQueryRequest 筛选简历请求类
     * @return 简历列表
     */
    @Override
    public Page<ApplicationVO> applicationScreenResume(ApplicationQueryRequest applicationQueryRequest) {
        int current = applicationQueryRequest.getCurrent();
        int pageSize = applicationQueryRequest.getPageSize();

        Page<ApplicationVO> pape = new Page<>(current,pageSize);
        applicationMapper.pageByCondition(pape,applicationQueryRequest);

        return pape;
    }
}




